package edu.yuhf.dao;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import edu.yuhf.dao.iface.NativePlaceDao;
import edu.yuhf.domain.NativePlace;

@Repository
public class NativePlaceJdbcDaoImpl implements NativePlaceDao {

	@Resource
	JdbcTemplate jdbcTemplate;
	@Override
	public List<NativePlace> getProvinces() {
		String sql="select * from nativePlace where length(code)=2";
		return jdbcTemplate.query(sql, (rs,index)->
			new NativePlace(rs.getInt(1),rs.getString(2),rs.getString(3))
		);
	}

	@Override
	public List<NativePlace> getCities(String provinceCode) {
		String sql="select * from nativePlace where length(code)=4 and substr(code,1,2)=?";
		return jdbcTemplate.query(sql, (rs,index)->
		new NativePlace(rs.getInt(1),rs.getString(2),rs.getString(3)),new Object[] {provinceCode});
	}

}
